#!python3

from tkinter import Tk, Label, Listbox, StringVar
from os import listdir
from os.path import isfile, join

folder = "music/"

root = Tk()

root.geometry("640x480")
status = StringVar()
status.set("init")

lbl = Label(root, textvariable=status)


def key(event):
    global listbox
    global status

    status.set("pressed %s" % repr(event.char))
    length = listbox.size()
    if length == 0:
        return

    cur = listbox.curselection()[0]
    if event.char == 'j':
        cur += 1
    elif event.char == 'k':
        cur -= 1 if cur > 0 else length - 1

    listbox.activate(cur % length)


listbox = Listbox(root)
listbox.bind("<Key>", key)
listbox.focus_set()

count = 0
for f in listdir(folder):
    fullpath = join(folder, f)
    print(fullpath)
    if isfile(fullpath):
        listbox.insert(count, f)
        count += 1

listbox.pack()
lbl.pack()

listbox.activate(1)

root.mainloop()
